#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

APP_TARGET_DIR_NAME="gpt-sovits"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_LFS_SKIP_SMUDGE=1

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/RVC-Boss/GPT-SoVITS"
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:30"

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.12
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y

echo "lmdprogress:40"

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    conda_run_conda_forge_install ffmpeg
    conda_run_pip_install onnxruntime==1.21.1
elif current_os_is_windows; then
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/check_ffmpeg_and_install_win.sh"
    if command -v nvidia-smi &>/dev/null; then
      echo "install onnxruntime-gpu"
      conda_run_pip_install onnxruntime-gpu==1.21.1
    else 
      conda_run_pip_install onnxruntime==1.21.1
    fi
fi

echo "lmdprogress:48"

conda_run_pip_install -r extra-req.txt
echo "lmdprogress:52"
if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    conda_run_pip_install -r requirements.txt
else
    grep -vE 'pyopenjtalk|jieba_fast|opencc' requirements.txt > requirements-temp.txt
    conda_run_pip_install -r requirements-temp.txt
    echo "lmdprogress:53"
    conda_run_pip_install opencc
    echo "lmdprogress:54"
    download_with_resume "pyopenjtalk-0.4.2-cp312-cp312-win_amd64.whl" "https://gitee.com/lmdown/pyopenjtalk/releases/download/v0.4.2/pyopenjtalk-0.4.2-cp312-cp312-win_amd64.whl"
    conda_run_pip_install pyopenjtalk-0.4.2-cp312-cp312-win_amd64.whl

    download_with_resume "jieba_fast-0.52-cp312-cp312-win_amd64.whl" "https://gitee.com/lmdown/jieba_fast/releases/download/v0.52/jieba_fast-0.52-cp312-cp312-win_amd64.whl"
    conda_run_pip_install jieba_fast-0.52-cp312-cp312-win_amd64.whl
fi
# echo "lmdprogress:47"
# conda_run_pip_install onnxruntime
echo "lmdprogress:55"
conda_run_pip_install huggingface_hub==0.34.6 hf_xet modelscope==1.30.0
echo "lmdprogress:60"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi
export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home"
export MODELSCOPE_HOME="$CURRENT_APP_PATH/tmp_modelscope_home"
export MODELSCOPE_CACHE="$CURRENT_APP_PATH/tmp_modelscope_cache"


MODEL_ID_AND_FILES="XXXXRT/GPT-SoVITS-Pretrained pretrained_models.zip G2PWModel.zip uvr5_weights.zip nltk_data.zip open_jtalk_dic_utf_8-1.11.tar.gz"
if [ -n "$HF_ENDPOINT" ]; then
    conda_run_modelsople_download "$MODEL_ID_AND_FILES" "./"
else 
    conda_run_hf_download "$MODEL_ID_AND_FILES"
fi

echo "lmdprogress:67"

conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_util.py" "pretrained_models.zip" "GPT_SoVITS"
echo "lmdprogress:70"


conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_util.py" "G2PWModel.zip" "GPT_SoVITS/text"
echo "lmdprogress:80"


conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_util.py" "uvr5_weights.zip" "tools/uvr5"
echo "lmdprogress:90"

PY_PREFIX=$(conda_run_cmd python -c "import sys; print(sys.prefix)")
conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_util.py" nltk_data.zip "$PY_PREFIX"

echo "lmdprogress:95"
PYOPENJTALK_PREFIX=$(conda_run_cmd python -c "import os, pyopenjtalk; print(os.path.dirname(pyopenjtalk.__file__))")
conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/unzip_tar_util.py" open_jtalk_dic_utf_8-1.11.tar.gz "$PYOPENJTALK_PREFIX"

echo "lmdprogress:100"
